<?php

        class Content_model extends CI_Model 
	{
		public function __construct()
		{
			parent::__construct();
			 
			// Load database
			$this->load->database();
                        
                        // Load session
                        $this->load->library('session');
		}
                
                public function get($ID)
                {
                    $query = $this->db->get_where('content', array('ID' => $ID));
			
                    if($query->num_rows() > 0)
                    {
                            return $query->row();
                    }
                    else
                    {
                            return NULL;
                    }
                }
         
                public function get_all($order = 'desc')
                {
                    $this->db->order_by('fetchedTime', $order);
                    $query = $this->db->get('content');
			
                    if($query->num_rows() > 0)
                    {
                            return $query->result();
                    }
                    else
                    {
                            return NULL;
                    }
                }
                
                public function get_latest($filter_id = NULL, $provider_name = NULL, $count = 10, $offset = 0, $order = 'desc')
                {
                    //var_dump($filter_id);
                    //var_dump($provider_name);
                    $user = unserialize($this->session->userdata('profile'));
                    
                    $this->db->select('     
                            content.ID,
                            content.title,
                            content.url,
                            content.text,
                            content.providerName,
                            content.fetchedTime,
                            clientFilter.title as filterTitle,
                            clientFilter.ID as filterID'
                    );
                    
                    $this->db->from('content');
                    $this->db->join('clientFilter', 'content.filterID = clientFilter.ID');
                    $this->db->join('users', 'clientFilter.clientID = users.ID');
                    
                    $this->db->where('users.ID', $user->ID);
                    
                    if($filter_id)
                        $this->db->where('clientFilter.ID', $filter_id);
                    
                    if($provider_name)
                        $this->db->where('content.providerName', $provider_name);
                    
                    $this->db->limit($count, $offset);
                    $this->db->order_by('fetchedTime', $order);
                    $query = $this->db->get();
                    
                    //var_dump($query);

                    if($query->num_rows() > 0)
                    {
                            return $query->result();
                    }
                    else
                    {
                            return NULL;
                    }
                }
                
                // Cron add
                public function add($object)
                {
                    $values = array(
				'filterID' => $object->getFilterID(),
				'title' => ( strlen( $object->getTitle() ) >= 256 ) ? substr($object->title, 0, 253) . '...' : $object->getTitle(),
				'url' => $object->getURL(),
                                'text' => ( $object->getText() ) ? 
                                          (( strlen( $object->getText() ) >= 256 ) ? substr($object->getText(), 0, 253) . '...' : $object->getText()) : '',
                                'providerName' => $object->getProviderName()
                    );
                    
                    $this->db->insert('content', $values); 
                    return $this->db->insert_id();
                }
                
                // Delete content associated with specified filter
		public function deleteFilterContent($filterID)
		{
                    return $this->db->delete('content', array('filterID' => $filterID)); 
		}
                
                // Delete content
		public function delete($object)
		{
                    return $this->db->delete('content', array('ID' => $object->ID)); 
		}
        }
     
?>
